#!/usr/bin/python

# This script is probably evil, but I am unspeakably sick of typing stuff like
# <span data-anolis-spec=dom title=concept-element-namespace>namespace</span>.
# <var title> is also really pointless, although I should really get that fixed
# in Anolis proper.
import re

replace = {
    'a': '<code data-anolis-spec=html title="the a element">a</code>',
    'activedocument': '<span data-anolis-spec=html title="active document">active document</span>',
    'ancestor': '<span data-anolis-spec=dom title=concept-tree-ancestor>ancestor</span>',
    'attribute': '<span data-anolis-spec=dom title=concept-attribute>attribute</span>',
    'attrlocalname': '<span data-anolis-spec=dom title=concept-attribute-local-name>local name</span>',
    'attrvalue': '<span data-anolis-spec=dom title=concept-attribute-value>value</span>',
    'b': '<code data-anolis-spec=html title="the b element">b</code>',
    'blockquote': '<code data-anolis-spec=html title="the blockquote element">blockquote</code>',
    'br': '<code data-anolis-spec=html title="the br element">br</code>',
    'boundarypoint': '<span data-anolis-spec=dom title=concept-range-bp>boundary point</span>',
    'bpafter': '<span data-anolis-spec=dom title=concept-range-bp-after>after</span>',
    'bpbefore': '<span data-anolis-spec=dom title=concept-range-bp-before>before</span>',
    'bpoffset': '<span data-anolis-spec=dom title=concept-range-bp-offset>offset</span>',
    'bpposition': '<span data-anolis-spec=dom title=concept-bp-position>position</span>',
    'browsingcontext': '<span data-anolis-spec=html title="browsing context">browsing context</span>',
    'cddata': '<code data-anolis-spec=dom title=dom-CharacterData-data>data</code>',
    'cdlength': '<code data-anolis-spec=dom title=dom-CharacterData-length>length</code>',
    'child': '<span data-anolis-spec=dom title=concept-tree-child>child</span>',
    'children': '<span data-anolis-spec=dom title=concept-tree-child>children</span>',
    'clonerange': '<code data-anolis-spec=dom title=dom-Range-cloneRange>cloneRange()</code>',
    'codeunit': '<a href=http://dev.w3.org/2006/webapi/WebIDL/#dfn-code-unit>code unit</a>',
    'collapsetoend': '<code title=dom-Selection-collapseToEnd>collapseToEnd()</code>',
    'collapsetostart': '<code title=dom-Selection-collapseToStart>collapseToStart()</code>',
    'collection': '<span data-anolis-spec=dom title=concept-collection>collection</span>',
    'contain': '<span data-anolis-spec=dom title=contained>contain</span>',
    'contained': '<span data-anolis-spec=dom>contained</span>',
    'comment': '<code data-anolis-spec=dom>Comment</code>',
    'contextobject': '<span data-anolis-spec=dom>context object</span>',
    'descendant': '<span data-anolis-spec=dom title=concept-tree-descendant>descendant</span>',
    'directionality': '<span data-anolis-spec=html title="the directionality">directionality</span>',
    'div': '<code data-anolis-spec=html title="the div element">div</code>',
    'dispatch': '<span data-anolis-spec=dom title=concept-event-dispatch>dispatch</span>',
    'dd': '<code data-anolis-spec=html title="the dd element">dd</code>',
    'dl': '<code data-anolis-spec=html title="the dl element">dl</code>',
    'dt': '<code data-anolis-spec=html title="the dt element">dt</code>',
    'doctype': '<span data-anolis-spec=dom title=concept-doctype>doctype</span>',
    'document': '<span data-anolis-spec=dom title=concept-document>document</span>',
    'documentfragment': '<code data-anolis-spec=dom>DocumentFragment</code>',
    'documenttype': '<code data-anolis-spec=dom>DocumentType</code>',
    'element': '<code data-anolis-spec=dom>Element</code>',
    'em': '<code data-anolis-spec=html title="the em element">em</code>',
    'endnode': '<span data-anolis-spec=dom title=concept-range-end-node>end node</span>',
    'endoffset': '<span data-anolis-spec=dom title=concept-range-end-offset>end offset</span>',
    'equivalent': '<span title="equivalent values">equivalent</span>',
    'extractcontents': '<code data-anolis-spec=dom title=dom-Range-extractContents>extractContents()</code>',
    'event': '<span data-anolis-spec=dom title=concept-event>event</span>',
    'firstchild': '<code data-anolis-spec=dom title=dom-Node-firstChild>firstChild</code>',
    'followingsibling': '<span data-anolis-spec=dom title="concept-tree-following">following</span> <span data-anolis-spec=dom title=concept-tree-sibling>sibling</span>',
    'font': '<code data-anolis-spec=html title=font>font</code>',
    'fontcolor': '<code data-anolis-spec=html title=dom-font-color>color</code>',
    'fontface': '<code data-anolis-spec=html title=dom-font-face>face</code>',
    'fontsize': '<code data-anolis-spec=html title=dom-font-size>size</code>',
    'getselection': '<code title=dom-Document-getSelection>getSelection()</code>',
    'htmldocument': '<code data-anolis-spec=html>HTMLDocument</code>',
    'htmlnamespace': '<span data-anolis-spec=dom>HTML namespace</span>',
    'hr': '<code data-anolis-spec=html title="the hr element">hr</code>',
    'href': '<code data-anolis-spec=html title=attr-hyperlink-href>href</code>',
    'i': '<code data-anolis-spec=html title="the i element">i</code>',
    'id': '<code data-anolis-spec=html title="the id attribute">id</code>',
    'img': '<code data-anolis-spec=html title="the img element">img</code>',
    'insert': '<span data-anolis-spec=dom title=concept-node-insert>insert</span>',
    'inclusiveancestor': '<span data-anolis-spec=dom title=concept-tree-inclusive-ancestor>inclusive ancestor</span>',
    'index': '<span data-anolis-spec=dom title=concept-tree-index>index</span>',
    'lastchild': '<code data-anolis-spec=dom title=dom-Node-lastChild>lastChild</code>',
    'length': '<span data-anolis-spec=dom title=concept-node-length>length</span>',
    'li': '<code data-anolis-spec=html title="the li element">li</code>',
    'localname': '<span data-anolis-spec=dom title=concept-element-local-name>local name</span>',
    'looselyequivalent': '<span title="loosely equivalent values">loosely equivalent</span>',
    'math': '<code data-anolis-spec=html>math</code>',
    'namespace': '<span data-anolis-spec=dom title=concept-element-namespace>namespace</span>',
    'nextsibling': '<code data-anolis-spec=dom title=dom-Node-nextSibling>nextSibling</code>',
    'node': '<span data-anolis-spec=dom title=concept-node>node</span>',
    'ol': '<code data-anolis-spec=html title="the ol element">ol</code>',
    'ownerdocument': '<code data-anolis-spec=dom title=dom-Node-ownerDocument>ownerDocument</code>',
    'p': '<code data-anolis-spec=html title="the p element">p</code>',
    'parent': '<span data-anolis-spec=dom title=concept-tree-parent>parent</span>',
    'partiallycontained': '<span data-anolis-spec=dom>partially contained</span>',
    'phrasingcontent': '<span data-anolis-spec=html>phrasing content</span>',
    'precedingsibling': '<span data-anolis-spec=dom title="concept-tree-preceding">preceding</span> <span data-anolis-spec=dom title=concept-tree-sibling>sibling</span>',
    'presentationalhint': '<span data-anolis-spec=html title="presentational hints">presentational hint</span>',
    'previoussibling': '<code data-anolis-spec=dom title=dom-Node-previousSibling>previousSibling</code>',
    'range': '<span data-anolis-spec=dom title=concept-range>range</span>',
    'rangecollapsed': '<code data-anolis-spec=dom title=dom-Range-collapsed>collapsed</code>',
    'rangeend': '<span data-anolis-spec=dom title=concept-range-end>end</span>',
    'rangemutationrules': '<span data-anolis-spec=dom>range mutation rules</span>',
    'rangeroot': '<span data-anolis-spec=dom title=concept-range-root>root</span>',
    'rangeset': '<span data-anolis-spec=dom title=concept-range-bp-set>set</span>',
    'rangestart': '<span data-anolis-spec=dom title=concept-range-start>start</span>',
    'remove': '<span data-anolis-spec=dom title=concept-node-remove>remove</span>',
    'removeallranges': '<code title=dom-Selection-removeAllRanges>removeAllRanges()</code>',
    'resval': '<a href=http://dev.w3.org/csswg/cssom/#resolved-value>resolved value</a>',
    'root': '<span data-anolis-spec=dom title=concept-tree-root>root</span>',
    's': '<code data-anolis-spec=html title="the s element">s</code>',
    'selcollapsed': '<span title=dom-Selection-isCollapsed>collapsed</span>',
    'seldir': '<span title=concept-selection-dir>direction</span>',
    'selection': '<span title=concept-selection>selection</span>',
    'sibling': '<span data-anolis-spec=dom title=concept-tree-sibling>sibling</span>',
    'spacecharacter': '<span data-anolis-spec=encoding title="ascii whitespace">space character</span>',
    'span': '<code data-anolis-spec=html title="the span element">span</code>',
    'startnode': '<span data-anolis-spec=dom title=concept-range-start-node>start node</span>',
    'startoffset': '<span data-anolis-spec=dom title=concept-range-start-offset>start offset</span>',
    'strike': '<code data-anolis-spec=html title="the strike element">strike</code>',
    'strlen': '<a href=http://es5.github.com/#x15.5.5.1>length</a>',
    'strong': '<code data-anolis-spec=html title="the strong element">strong</code>',
    'style': '<code data-anolis-spec=html title="the style attribute">style</code>',
    'sub': '<code data-anolis-spec=html title="the sub and sup elements">sub</code>',
    'sup': '<code data-anolis-spec=html title="the sub and sup elements">sup</code>',
    'svg': '<code data-anolis-spec=html>svg</code>',
    'table': '<code data-anolis-spec=html title="the table element">table</code>',
    'tbody': '<code data-anolis-spec=html title="the tbody element">tbody</code>',
    'td': '<code data-anolis-spec=html title="the td element">td</code>',
    'tfoot': '<code data-anolis-spec=html title="the tfoot element">tfoot</code>',
    'th': '<code data-anolis-spec=html title="the th element">th</code>',
    'thead': '<code data-anolis-spec=html title="the thead element">thead</code>',
    'throw': '<span data-anolis-spec=dom title=concept-throw>throw</span>',
    'tr': '<code data-anolis-spec=html title="the tr element">tr</code>',
    'text': '<code data-anolis-spec=dom>Text</code>',
    'treeorder': '<span data-anolis-spec=dom title=concept-tree-order>tree order</span>',
    'u': '<code data-anolis-spec=html title="the u element">u</code>',
    'ul': '<code data-anolis-spec=html title="the ul element">ul</code>',
    'IndexSizeError': '<code data-anolis-spec=dom>IndexSizeError</code>',
    'InvalidAccessError': '<code data-anolis-spec=dom>InvalidAccessError</code>',
    'InvalidModificationError': '<code data-anolis-spec=dom>InvalidModificationError</code>',
    'InvalidNodeTypeError': '<code data-anolis-spec=dom>InvalidNodeTypeError</code>',
    'InvalidStateError': '<code data-anolis-spec=dom>InvalidStateError</code>',
    'NotSupportedError': '<code data-anolis-spec=dom>NotSupportedError</code>',
    'SecurityError': '<code data-anolis-spec=dom>SecurityError</code>',
    'SyntaxError': '<code data-anolis-spec=dom>SyntaxError</code>',
}

s = open("source.html", "r").read()
for key in replace:
    s = s.replace("[[" + key + "]]", replace[key])
    # Plurals
    s = s.replace("[[" + key + "s]]", replace[key].replace("</", "s</"))
    s = s.replace("[[" + key + "es]]", replace[key].replace("</", "es</"))
    # Capitals
    capreplace = replace[key].split(">")
    capreplace[1] = capreplace[1].capitalize()
    capreplace = ">".join(capreplace)
    s = s.replace("[[" + key.capitalize() + "]]", capreplace)
    # Capital plurals
    s = s.replace("[[" + key.capitalize() + "s]]", capreplace.replace("</", "s</"))

fnreplace = {
    'addrange': '<code title=dom-Selection-addRange>addRange(\\1)</code>',
    'appendchild': '<code data-anolis-spec=dom title=dom-Node-appendChild>appendChild(\\1)</code>',
    'createelement': '<code data-anolis-spec=dom title=dom-Document-createElement>createElement(\\1)</code>',
    'createtextnode': '<code data-anolis-spec=dom title=dom-Document-createTextNode>createTextNode(\\1)</code>',
    'deletedata': '<code data-anolis-spec=dom title=dom-CharacterData-deleteData>deleteData(\\1)</code>',
    'execcommand': '<code title=execCommand()>execCommand(\\1)</code>',
    'extend': '<code title=dom-Selection-extend>extend(\\1)</code>',
    'insertdata': '<code data-anolis-spec=dom title=dom-CharacterData-insertData>insertData(\\1)</code>',
    'insertnode': '<code data-anolis-spec=dom title=dom-Range-insertNode>insertNode(\\1)</code>',
    'replacedata': '<code data-anolis-spec=dom title=dom-CharacterData-replaceData>replaceData(\\1)</code>',
    'selcollapse': '<code title=dom-Selection-collapse>collapse(\\1)</code>',
    'selectallchildren': '<code title=dom-Selection-selectAllChildren>selectAllChildren(\\1)</code>',
    'setattribute': '<code data-anolis-spec=dom title=dom-Element-setAttribute>setAttribute(\\1)</code>',
    'splittext': '<code data-anolis-spec=dom title=dom-Text-splitText>splitText(\\1)</code>',
}

for key in fnreplace:
    s = re.sub(r"\[\[" + key + "\|([^]]*)\]\]", fnreplace[key], s)

if "[[" in s:
    raise Exception("Unrecognized macro: " + s[s.find("[["):s.find("]]") + 2])

# {{html|<foo>}} -> &lt;foo>, no wrapper.  Also, to avoid things like
# {{code|<b>}} messing up syntax highlighting, {{html<|b>}} is also supported
# with the same effect as {{html|<b>}}.  Trailing whitespace before the }} will
# be ignored, so that {{html|} }} will produce "}", since {{html|}}} won't work
# (the first two "}"'s will terminate the expression).
def htmlsub(matchobj):
    return matchobj.group(1).replace("<", "&lt;") \
         + matchobj.group(2).replace("&", "&amp;").replace("<", "&lt;")

# {{code|...}} is just a shortcut for <code title>{{html|...}}</code>.
def codesub(matchobj):
    return "<code title>" \
        + htmlsub(matchobj) \
        + "</code>"

s = re.compile(r"\{\{html(<?)\|(.*?)\s*\}\}", re.DOTALL).sub(htmlsub, s)
s = re.compile(r"\{\{code(<?)\|(.*?)\s*\}\}", re.DOTALL).sub(codesub, s)

s = s.replace("<var>", "<var title>")

s = s.replace("<!-- @{ -->", "")
s = s.replace("<!-- @} -->", "")
s = re.sub(r"\n<!-- vim:.*: -->", "", s)

f = open("intermediate.html", "w")
f.write(s)
f.close()
# vim: set textwidth=0:
